﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using zdzh.zdmgr.BLL;
using zdzh.zdmgr.COL;
using zdzh.zdmgr.SFL;

namespace zdzh.zdmgr.UIL.huiqianxieyip
{
    /// <summary>
    /// 对象名称：“回迁协议模块”修改信息页面（用户界面层）
    /// 对象说明：该页面用于修改“回迁协议”对象并更新至数据库，操作完成后返回列表页面。
    /// 作者姓名：飞翔熊
    /// 编写日期：2012/12/3 13:23:16
    /// </summary>
    public partial class Detail : System.Web.UI.Page
    {
        protected static int CurIndex;
        protected static Huiqianxieyi huiqianxieyi;
        protected static List<Huiqianhu> huiqianhu;
        protected static List<Huiqiandianhua> huiqiandianhua;
        protected static List<Huiqianfangzhao> huiqianfangzhao;
        protected static List<Fangyuan> fangyuan;      //  房源 - 是否已经回迁

        protected static List<Fangyuan> fangyuanFree;  //  空闲房源
        protected static List<Fangyuan> fangyuanFreeFilter=new List<Fangyuan>();  //  空闲房源 筛选用

        /// <summary>
        /// “回迁协议模块”修改信息页面载入时的事件处理方法。
        /// </summary>
        protected void Page_Load(object sender, EventArgs e)
        {
            if ( !Page.IsPostBack)
            {
                GetXieyiData();
                BindRepGridData();
            }
        }

        private void GetXieyiData()
        {
            CurIndex = Convert.ToInt32(Request.QueryString["Index"]);
            huiqianxieyi = HuiqianxieyiBLL.GetDataByIndex(CurIndex); // 从数据库中获取关联对象信息，以备进行修改操作。
            if (huiqianxieyi == null)
                throw new CustomException("对不起，所要查看的信息不存在或已删除。");
            huiqianhu = HuiqianhuBLL.GetDataByXieyihao(huiqianxieyi.Xieyihao.Value);
            huiqiandianhua = HuiqiandianhuaBLL.GetDataByXieyihao(huiqianxieyi.Xieyihao.Value);
            huiqianfangzhao = HuiqianfangzhaoBLL.GetDataByXieyihao(huiqianxieyi.Xieyihao.Value);
            fangyuan = FangyuanBLL.GetDataByXieyiIndex(huiqianxieyi.Xieyihao.Value);
        }

        private void BindRepGridData()
        {
            RepGridHuiqianhu.DataSource = huiqianhu;
            RepGridHuiqianhu.DataBind();

            RepGridDianhua.DataSource = huiqiandianhua;
            RepGridDianhua.DataBind();

            RepGridFangzhao.DataSource = huiqianfangzhao;
            RepGridFangzhao.DataBind();

            RepGridFangyuan.DataSource = fangyuan;
            RepGridFangyuan.DataBind();
        }

        protected string cs_showbuild(object build, object unit, object floor, object room)
        {
            return WebShow.WS_build(build, unit, floor, room);
        }

        protected void BtnAddFangyuan_Click(object sender, EventArgs e)
        {
            //添加回迁房源
            if (DdlType.SelectedIndex == 0)
                return;
            if (DdlRoom.SelectedItem.Value == null)
                return;

            foreach (Fangyuan fang in fangyuanFreeFilter)
            {
                if (fang.Index.ToString() == DdlRoom.SelectedItem.Value)
                {
                    fang.XieyiIndex = huiqianxieyi.Index;
                    fang.State = 3;
                    FangyuanBLL.Update(fang);
                    GetXieyiData();
                    BindRepGridData();
                    RemoveFangFromFangyuanList(fang.Index);
                    DdlUnit_SelectedIndexChanged(null, null);
                    break;
                }
            }
        }

        protected void RemoveFangFromFangyuanList(int index)
        {
            for (int i = 0; i < fangyuanFree.Count; )
            {
                if ( fangyuanFree[i].Index==index)
                {
                    fangyuanFree.Remove(fangyuanFree[i]);
                }
                else
                    i++;
            }

            for (int i = 0; i < fangyuanFreeFilter.Count; )
            {
                if (fangyuanFreeFilter[i].Index == index)
                {
                    fangyuanFreeFilter.Remove(fangyuanFreeFilter[i]);
                }
                else
                    i++;
            }
        }

        protected void RepGridFangyuan_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "BtnDelFangyua")
            {
                int? index = DataValid.GetNullOrInt(e.CommandArgument.ToString());
                if (index != null)
                    BtnDelFangyuan_Click(index.Value);
            }
        }
        protected void BtnDelFangyuan_Click(int index)
        {
            Fangyuan fang = FangyuanBLL.GetDataByIndex(index);
            if (fang == null)
            {
                Response.Write("<script language=javascript>alert('未找到房源记录！');</script>"); 
            }
            else
            {
                fang.XieyiIndex = null;
                FangyuanBLL.Update(fang);
                GetXieyiData();
                BindRepGridData();
            }
        }

        protected void DdlType_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (DdlType.SelectedIndex == 0)
                return;
            switch (DdlType.SelectedIndex)
            {
                case 1:
                case 4:
                case 5:
                    DdlUnit.Visible = true;
                    LblUnit.Visible = true;
                    break;

                case 2:
                case 3:
                    DdlUnit.Visible = false;
                    LblUnit.Visible = false;
                    break;
            }
            fangyuanFree = FangyuanBLL.GetDataByType(DdlType.SelectedIndex, 1);
            
            List<string> strBuild=new List<string>();
            foreach (Fangyuan fang in fangyuanFree)
            {
                bool bFind = false;
                string strCmp = fang.Build.ToString();
                foreach (string str in strBuild)
                {
                    if (strCmp == str)
                    {
                        bFind = true;
                        break;
                    }
                }
                if (!bFind)
                    strBuild.Add(strCmp);
            }
            strBuild = DataValid.SortBuild(strBuild);
            DdlBuild.Items.Clear();
            foreach (string str in strBuild)
            {
                DdlBuild.Items.Add(str);
            }
            DdlBuild_SelectedIndexChanged(null, null);
        }

        protected void DdlBuild_SelectedIndexChanged(object sender, EventArgs e)
        {
            fangyuanFreeFilter.Clear();
            string strBuild = DdlBuild.SelectedItem.Text;
            DdlUnit.Items.Clear();
            DdlRoom.Items.Clear();

            foreach (Fangyuan fang in fangyuanFree)
            {
                if (fang.Build != strBuild)
                    continue;
                fangyuanFreeFilter.Add(fang);

                if (DdlType.SelectedIndex == 1
                || DdlType.SelectedIndex == 4
                || DdlType.SelectedIndex == 5)
                {
                    string strCmp = fang.Unit.ToString();
                    bool bFind = false;
                    foreach (ListItem item in DdlUnit.Items)
                    {
                        if (item.Text == strCmp)
                        {
                            bFind = true;
                            break;
                        }
                    }
                    if (!bFind)
                        DdlUnit.Items.Add(strCmp);
                }
                else if ( DdlType.SelectedIndex ==2 || DdlType.SelectedIndex==3)
                {
                    DdlRoom.Items.Add(new ListItem(fang.Room.ToString(),fang.Index.ToString()));
                }
            }
            if (DdlType.SelectedIndex == 1
                || DdlType.SelectedIndex == 4
                || DdlType.SelectedIndex == 5)
                DdlUnit_SelectedIndexChanged(null, null);

            DdlRoom_SelectedIndexChanged(null, null);
        }

        protected void DdlUnit_SelectedIndexChanged(object sender, EventArgs e)
        {
            DdlRoom.Items.Clear();
            string strUnit=DdlUnit.SelectedItem.Value;
            foreach (Fangyuan fang in fangyuanFreeFilter)
            {
                if (strUnit != fang.Unit.ToString())
                    continue;
                string strCmp = fang.Floor.ToString()+"0"+fang.Room.ToString();
                bool bFind = false;
                foreach (ListItem item in DdlRoom.Items)
                {
                    if (item.Text == strCmp)
                    {
                        bFind = true;
                        break;
                    }
                }
                if (!bFind)
                    DdlRoom.Items.Add(new ListItem(strCmp,fang.Index.ToString()));
            }
        }

        protected void DdlRoom_SelectedIndexChanged(object sender, EventArgs e)
        {
            string strIndex=DdlRoom.SelectedItem.Value;
            foreach ( Fangyuan fang in fangyuanFreeFilter)
            {
                if ( fang.Index.ToString()==strIndex)
                {
                    if (fang.Designarea == null)
                        LblArea.Text = "---";
                    else
                        LblArea.Text = fang.Designarea.ToString();

                    LblArea.Text += "／";

                    if (fang.Area == null)
                        LblArea.Text += "---";
                    else
                        LblArea.Text += fang.Area.ToString();

                    break;
                }
            }
        }

        protected void BtnGoIndex_Click(object sender, EventArgs e)
        {
            if (!DataValid.IsInt(TxtGoIndex.Text))
                throw new CustomException("“索引”不能为空，请您确认输入是否正确。");
            Response.Redirect("Detail.aspx?Index="+TxtGoIndex.Text);
        }
    }
}
